<?php
if (!empty($dataProject)){
    foreach ($dataProject as $k => $v) {
        //----项目----start---
        if ($v['_index'] == "project"){
            $project_pid           = !empty($v['_source']['package_id'])?$v['_source']['package_id']:0;
            $project_title         = !empty($v['_source']['project_title'])?$v['_source']['project_title']:"";        //标题
            $project_borrower      = !empty($v['_source']['borrower'])?$v['_source']['borrower']:""; //持包人名称
            $project_code      = !empty($v['_source']['code'])?$v['_source']['code']:""; //项目编号
            $project_principal      = !empty($v['_source']['principal'])?\common\helps\formatMoney::format_money($v['_source']['principal']):""; //项目本金
            $project_interest      = !empty($v['_source']['interest'])?\common\helps\formatMoney::format_money($v['_source']['interest']):""; //项目利息
            $project_created_at      = !empty($v['_source']['created_at'])?$v['_source']['created_at']:""; //项目利息
            $timeCreate = mb_substr($project_created_at,0,mb_stripos($project_created_at,'T'));
            $project_status      = !empty(Yii::$app->params['projectState'][$v['_source']['status']]['name'])?Yii::$app->params['projectStatus'][$v['_source']['status']]['name']:"";//状态

            $cityId = !empty($v['_source']['city'])?$v['_source']['city']:""; //城市ID
            $provinceId = !empty($v['_source']['city'])?$v['_source']['city']:""; //省份ID
            $project_city = "";
            $project_province = ''; //省份
            if (!empty($cityId)){
                $project_city = \backend\models\SearchPackage::getCity($cityId)[0]['title'];     //城市
            }
            if (!empty($provinceId)){
                $project_province = \backend\models\SearchPackage::getCity($provinceId)[0]['title'];     //城市
            }

            //------项目所在资产包
            $packageInfo = \backend\models\SearchPackage::getPackageOfPackId($project_pid);
            $packageOwner = !empty($packageInfo['owner'])?$packageInfo['owner']:'';
            $packageTitle = !empty($packageInfo['title'])?$packageInfo['title']:'';
            $packageCreditor = !empty($packageInfo['creditor'])?$packageInfo['creditor']:'';

            //-----项目经理-----start--
            $manager  = $managerId = $owner = $description1 = $description2= "";
            if (!empty($project_team)){
                foreach ($project_team as $k1 => $v1) {
                    if ($v1['_source']['project_id'] == $v["_id"]){
                        $managerId = !empty($v1['_source']['owner'])?str_replace(',', '', $v1['_source']['owner']):""; //资产包负责人ID
                    }
                }
                if (!empty($managerId)&&!empty($user)){
                    foreach ($user as $k2 => $v2) {
                        if ($v2['_id'] == $managerId){
                            $manager = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                            if (!empty($auth_item)&&!empty($manager)){
                                foreach ($auth_item as $k3 => $v3){
                                    if ($v3['_source']['name'] == $v2['_source']['department']){
                                        $description1 = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $leaderId = !empty($packageOwner)?$packageOwner:""; //上传人
            if (!empty($leaderId)&&!empty($user)){
                foreach ($user as $k2 => $v2) {
                    if ($v2['_id'] == $leaderId){
                        $owner = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                        if (!empty($auth_item)){
                            foreach ($auth_item as $k3 => $v3){
                                if ($v3['_source']['name'] == $v2['_source']['department']){
                                    $description2 = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                }
                            }
                        }
                    }
                }
            }
//            $manager = !empty($manager)?$manager:$owner;
            //-----项目经理-----end--
            //----- 项目协助人员---start---
            $partner = '';
            if (!empty($project_team)){
                $partner_id = "";
                foreach ($project_team as $k1 => $v1) {
                    if ($v1['_source']['project_id'] == $v["_id"]){
                        $partner_id = !empty($v1['_source']['partner'])?$v1['_source']['partner']:""; //资产包负责人ID
                    }
                }
                if (!empty($partner_id)&&!empty($user)){
                    $partner_id_arr = explode(',',$partner_id);
                    foreach ($partner_id_arr as $k3 => $v3) {
                        foreach ($user as $k2 => $v2) {
                            if ($v2['_id'] == $v3){
                                $partner.= !empty($v2['_source']['realname'])?$v2['_source']['realname'].',':""; //资产包负责人
                            }
                        }
                    }
                }
            }
            //-----项目协助人员-----end--
            //---table-------start---
            //---权限
            $filterProject = \backend\controllers\DtActionFilter::checkProject("project_view",$v['_id']);
            $Redproject_title = preg_replace($normalStr, $tagStr, $project_title);
            $project_id = $v['_id'];
            if ($filterProject){
                $projectUrl = "<a href='/project/getinfodata?package_id=$project_pid&project_id=$project_id' target='_blank'><span class='pull-left'>".$Redproject_title."</span></a>";
                $prijectFilterHtml = "";
            }else{
                $prijectFilterHtml = '<div class="pull-right permission">无访问权限</div>';
                $projectUrl = <<<PROJECTUTLHTML
<a onclick="lmsgPro($project_id,'$project_title',2)"><span class='pull-left'>$Redproject_title</span></a>
PROJECTUTLHTML;
            }

            //--隶属资产包权限
            $filterPackage = \backend\controllers\DtActionFilter::checkPackage("package_view",$project_pid);
            if ($filterPackage){
                $packageUrl = <<<PACKAGEURLHTML
<a href="/package/detail-info?packageid=$project_pid">$packageTitle</a>
PACKAGEURLHTML;
            }else{
                $packageUrl = <<<PACKAGEURLHTML
<a onclick="lmsgPro($project_pid,'$packageTitle',1)">$packageTitle</a>
PACKAGEURLHTML;
            }
            //--无状态不显示状态标签
            if (!empty($project_status)&&$v['_source']['status']>1){
                $statesHtml = '<div class="pull-right project">'.$project_status.'</div>';
            }else{
                $statesHtml = '';
            }
            echo <<<TABLEPROJECT
            <div class="searchLists clearfix searchLists-pakage">
                 <div class="searchLists-nav clearfix">
                                  $projectUrl
                    <div class="pull-left package">项目</div>
                    $prijectFilterHtml
                    $statesHtml
                </div>
                <div class="searchLists-content">
                    <span>持包人名称：<font>$packageCreditor</font></span>
                    <span>本金：	<font>$project_principal</font></span>
                    <span>利息： <font>$project_interest</font></span>
                    <span>省份地区： <font> $project_province - $project_city</font></span></br>
                    <span class="lszcb">隶属资产包：$packageUrl </span>
                    <span>项目经理： <font>$description1 - $manager</font></span>
                    <span>团队成员： <font>$partner</font></span>
         		</div>
                <div class="sc">
                    <span class="pull-right"> $timeCreate $owner 上传</span>
                </div>

           </div>
TABLEPROJECT;
            //---table-------end---
        }
        //----项目----end---
    }
}
?>
<script>
    function lmsgPro(id,title,is_package) {
        var title = title;
        $("#packageId").val(id);
        $("#shenqingFW").modal();
        $("#title").val(title)
        $("#is_package").val(is_package)
    }
    //------------------------------申请访问----fly------------------
    $(function () {
        $("#saveFWAccess").click(function () {
            var shenqingFWNote = $("#shenqingFWNote").val();
            var packageId = $("#packageId").val();
            var title = $("#title").val();
            var is_package = $("#is_package").val();
            var group = 2;
            if (is_package==1){
                group = 1
            }else{
                group = 2
            }
            var url = '/package-apply/access-apply'
            if (shenqingFWNote!=''){
                $.post(url,{'packageId':packageId,'shenqingFWNote':shenqingFWNote,'title':title,'group':group,'is_package':is_package},function (res) {
                    resData = JSON.parse(res)
                    layer.msg(resData.message)
                    if (resData.code == 200){
                        $("#shenqingFW").modal('hide');
                    }
                })
            }else{
                layer.msg('请填写访问申请理由');
            }
        })
    })
    //------------------------------申请访问----fly------------------

</script>



